<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_model extends CI_Model {
	
	function validate()
	{
		$this->db->where('username', $this->input->post('username'));
		$this->db->where('password', md5($this->input->post('password')));
		$query = $this->db->get('users');
		
		if ($query->num_rows == 1) {
		$validate = true;
			return true;
		}
	}
	
	function create_member() {
		$new_member_insert_data = array(
			'username' => $this->input->post('username'),
			'password' => md5($this->input->post('password')),
			'name' => $this->input->post('name'),
			'lastname' => $this->input->post('lastname'),
			'user_type' => $this->input->post('user_type')
		);
		
		$insert = $this->db->insert('users', $new_member_insert_data);
		return $insert;
	}
	
	function login($username, $password) {
	
		$this -> db -> select('user_id,username,password,usertype_id');
		$this -> db -> from('users');
		$this -> db -> where('username',$username);
		$this -> db -> where('password',$password);
		$this -> db -> limit(1);
	
		$query = $this -> db -> get();
	
		if($query -> num_rows() == 1){
		
			return $query->result();
		}
		else{
		
			return false;
		}
	}

	function insert_user($data) {
		$this->db->insert('users',$data);	
	}
	
	function update_data_user($data,$usertype_id) {
	
		$this->db->where('user_id',$usertype_id);
		$this->db->update('users',$data);
	}
		
	function delete_data_user($id) {
		
		$this->db->where('user_id',$id);
		$this->db->delete('users');
	}
		
		
	function select_profile($usertype_id){
			
		$this -> db -> select('*');
		$this -> db -> from('users');
		$this -> db -> where('users.usertype_id',$usertype_id);
		$this->db->join('usertype', 'users.usertype_id = usertype.usertype_id');
		$query = $this -> db -> get();
		return $query->row_array();
	}
	
	function select_all() {
	
		$this->db->select('*');
		$this->db->from('users');
		$this->db->join('usertype','users.usertype_id = usertype.usertype_id','left');
		$query = $this -> db -> get();
		return $query->result_array();
	}
	
	function _getAll(){
	
		$result = $this->db->get('users')->result();
		return $result;
	}
	
	function _getDetail($user_type){
	
		$result = $this->db->where('user_type', $user_type)->get('usertype')->row();
		return $result;
	}
}